package com.peanut.baby.mvp.main.expert.live;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.LiveRoom;
import com.peanut.baby.mvp.livedetail.LiveDetailActivity;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/7/28.
 */

public class LiveListAdapter extends HolderAdapter<LiveRoom,LiveListAdapter.Holder>{

    private OnLiveRoomJoinListener listener;
    public LiveListAdapter(Context context, List<LiveRoom> listData) {
        super(context, listData);
    }

    public void setOnLiveRoomJoinListener(OnLiveRoomJoinListener lis){
        this.listener = lis;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, LiveRoom object, int position) {
        return layoutInflater.inflate(R.layout.layout_item_live_list,null);
    }

    @Override
    public Holder buildHolder(View convertView, LiveRoom object, int position) {
        Holder holder = new Holder();
        holder.root = convertView;
        holder.title = (TextView) convertView.findViewById(R.id.live_title);
        holder.expertName = (TextView) convertView.findViewById(R.id.live_expert_name);
        holder.expertTitle = (TextView) convertView.findViewById(R.id.live_expert_title);
        holder.expertHospital = (TextView) convertView.findViewById(R.id.live_expert_hospital);
        holder.status = (TextView) convertView.findViewById(R.id.live_status);
        holder.time = (TextView) convertView.findViewById(R.id.live_time);
        holder.enroled = (TextView) convertView.findViewById(R.id.live_enroled);
        holder.enroleCount = (TextView) convertView.findViewById(R.id.live_enrole_count);
        holder.image = (ImageView) convertView.findViewById(R.id.live_image);
        holder.price = (TextView) convertView.findViewById(R.id.live_price);
        holder.join = (TextView) convertView.findViewById(R.id.live_join);
        return holder;
    }

    @Override
    public void bindViewDatas(Holder holder, final LiveRoom item, int position) {

        holder.title.setText(item.title);
        holder.expertName.setText(item.expertName);
        holder.expertHospital.setText(item.expertHospital);
        holder.expertTitle.setText(item.expertJobTitle);
        holder.time.setText(item.getTimeString());
        holder.status.setText(item.getStatusString());
        holder.enroled.setText("已报名"+item.enrolCount);
        holder.enroleCount.setText("/"+item.allowCount);
        holder.price.setText(item.getPriceString());


        if(item.expertId.equals(InitManager.getInstance().getUserId())
                || item.serverId.equals(InitManager.getInstance().getUserId())){
            holder.join.setText("我的直播");
        } else {
            if(item.isEnroled == 0){
                holder.join.setText("立即报名");
            }else{
                if(item.roomStatus == LiveRoom.ROOM_STATUS_FINISHED){
                    holder.join.setText("回放中");
                }else{
                    holder.join.setText("进入直播间");
                }

            }
        }

        ImageLoader.getInstance().loadImageView(holder.title.getContext(),item.roomImageUrl,holder.image);

        holder.root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveDetailActivity.start(context,item);
            }
        });

        holder.join.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onLiveRoomJoin(item);
                }
            }
        });
    }

    class Holder{
        View root;
        TextView title;
        TextView expertName;
        TextView expertTitle;
        TextView expertHospital;
        TextView status;
        TextView time;
        TextView enroled;
        TextView enroleCount;
        ImageView image;
        TextView price;
        TextView join;

    }

    public interface OnLiveRoomJoinListener{
        void onLiveRoomJoin(LiveRoom room);
    }
}
